/**
 * @file inOutCtrl.c
 * @brief   
 * @author  xiaowine (xiaowine@sina.cn)
 * @version 01.00
 * @date    2020-12-10
 * 
 * @copyright Copyright (c) {2020}  xiaowine
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2020-12-10 <td>1.0     <td>wangh     <td>内容
 * </table>
 * ******************************************************************
 * *                   .::::
 * *                 .::::::::
 * *                ::::::::::
 * *             ..:::::::::::
 * *          '::::::::::::
 * *            .:::::::::
 * *       '::::::::::::::..        女神助攻,流量冲天
 * *            ..::::::::::::.     永不宕机,代码无bug
 * *          ``:::::::::::::::
 * *           ::::``:::::::::'        .:::
 * *          ::::'   ':::::'       .::::::::
 * *        .::::'      ::::     .:::::::'::::
 * *       .:::'       :::::  .:::::::::' ':::::
 * *      .::'        :::::.:::::::::'      ':::::
 * *     .::'         ::::::::::::::'         ``::::
 * * ...:::           ::::::::::::'              ``::
 * *```` ':.          ':::::::::'                  ::::.
 * *                   '.:::::'                    ':'````.
 * ******************************************************************
 */

#include "inOutCtrl.h"
#include "opration.h"

void inOutCtrlInit(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;

    /* 第1步：打开GPIO时钟 */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG, ENABLE);

    /* 第2步：配置所有的按键GPIO为浮动输入模式(实际上CPU复位后就是输入状态) */
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; /* 输入浮空模式 */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
    GPIO_Init(GPIOD, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_9 | GPIO_Pin_11;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_Init(GPIOF, &GPIO_InitStructure);

    /* 第2步：配置所有的按键GPIO为浮动输入模式(实际上CPU复位后就是输入状态) */
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 推挽输出 */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
    GPIO_Init(GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_Init(GPIOE, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
    GPIO_Init(GPIOG, &GPIO_InitStructure);

    PEout(0) = outPutOff;
    PBout(9) = outPutOff;
    PBout(8) = outPutOff;
    PBout(5) = outPutOff;
    PGout(15) = outPutOff;
    PGout(14) = outPutOff;
    PGout(13) = outPutOff;
    PGout(12) = outPutOff;
    PAout(11) = outPutOff;
    PAout(10) = outPutOff;
    PAout(9) = outPutOff;
    PAout(8) = outPutOff;
    PCout(9) = outPutOff;
    PCout(8) = outPutOff;
    PGout(8) = outPutOff;
}

void inOutCtrl(void)
{
    u8 fanFlag = 0;
    /***加热区 1 2 3 4************/
    if (PEin(2) == inPutOn)
    {
        PEout(0) = outPutOn;
        fanFlag = 1;
        if (PCin(7) == inPutOff)
        {
            if (heaterZoneCount[0] < optData.heaterOnRange[0])
            {
                PBout(9) = outPutOn;
            }
            else if (heaterZoneCount[0] < (optData.heaterOnRange[0] + optData.heaterOffRange[0]))
            {
                PBout(9) = outPutOff;
            }
            else
            {
                heaterZoneCount[0] = 0;
            }
        }
        else
        {
            PBout(9) = outPutOff;
        }
    }
    else
    {
        PEout(0) = outPutOff;
        PBout(9) = outPutOff;
    }

    if (PEin(3) == inPutOn)
    {
        PBout(8) = outPutOn;
        fanFlag = 1;
        if (PCin(6) == inPutOff)
        {
            if (heaterZoneCount[1] < optData.heaterOnRange[1])
            {
                PBout(5) = outPutOn;
            }
            else if (heaterZoneCount[1] < (optData.heaterOnRange[1] + optData.heaterOffRange[1]))
            {
                PBout(5) = outPutOff;
            }
            else
            {
                heaterZoneCount[1] = 0;
            }
        }
        else
        {
            PBout(5) = outPutOff;
        }
    }
    else
    {
        PBout(8) = outPutOff;
        PBout(5) = outPutOff;
    }

    if (PEin(4) == inPutOn)
    {
        PGout(15) = outPutOn;
        fanFlag = 1;
        if (PDin(13) == inPutOff)
        {
            if (heaterZoneCount[2] < optData.heaterOnRange[2])
            {
                PGout(14) = outPutOn;
            }
            else if (heaterZoneCount[2] < (optData.heaterOnRange[2] + optData.heaterOffRange[2]))
            {
                PGout(14) = outPutOff;
            }
            else
            {
                heaterZoneCount[2] = 0;
            }
        }
        else
        {
            PGout(14) = outPutOff;
        }
    }
    else
    {
        PGout(15) = outPutOff;
        PGout(14) = outPutOff;
    }

    if (PEin(5) == inPutOn)
    {
        PGout(13) = outPutOn;
        fanFlag = 1;
        if (PDin(12) == inPutOff)
        {
            if (heaterZoneCount[3] < optData.heaterOnRange[3])
            {
                PGout(12) = outPutOn;
            }
            else if (heaterZoneCount[3] < (optData.heaterOnRange[3] + optData.heaterOffRange[3]))
            {
                PGout(12) = outPutOff;
            }
            else
            {
                heaterZoneCount[3] = 0;
            }
        }
        else
        {
            PGout(12) = outPutOff;
        }
    }
    else
    {
        PGout(13) = outPutOff;
        PGout(12) = outPutOff;
    }
    if (fanFlag)
    {
        PCout(8) = outPutOn;
    }
    else
    {
        PCout(8) = outPutOff;
    }

    {
        u8 tempretureWarnFlag = 0;
        //热容釜加热
        if (PEin(6) == inPutOn)
        {
            if ((tempreture[0] < optData.tempretureStart[0]) & (PEin(11) == inPutOff))
            {
                PAout(11) = outPutOn;
            }
            if ((tempreture[0] > optData.tempretureStop[0]) || (PEin(11) == inPutOn))
            {
                PAout(11) = outPutOff;
            }
            if (tempreture[0] > optData.tempretureWarn[0])
            {
                tempretureWarnFlag = 1;
            }
        }
        else
        {
            PAout(11) = outPutOff;
        }
        //搅拌器加热1
        if (PCin(13) == inPutOn)
        {
            if ((tempreture[1] < optData.tempretureStart[1]) & (PEin(9) == inPutOff))
            {
                PAout(10) = outPutOn;
            }
            if ((tempreture[1] > optData.tempretureStop[1]) || (PEin(9) == inPutOn))
            {
                PAout(10) = outPutOff;
            }
            if (tempreture[1] > optData.tempretureWarn[1])
            {
                tempretureWarnFlag = 1;
            }
        }
        else
        {
            PAout(10) = outPutOff;
        }
        //搅拌器加热2
        if (PCin(14) == inPutOn)
        {
            if ((tempreture[2] < optData.tempretureStart[2]) & (PAin(7) == inPutOff))
            {
                PAout(9) = outPutOn;
            }
            if ((tempreture[2] > optData.tempretureStop[2]) || (PAin(7) == inPutOn))
            {
                PAout(9) = outPutOff;
            }
            if (tempreture[2] > optData.tempretureWarn[2])
            {
                tempretureWarnFlag = 1;
            }
        }
        else
        {
            PAout(9) = outPutOff;
        }
        //输送管加热
        if (PCin(15) == inPutOn)
        {
            if ((tempreture[3] < optData.tempretureStart[3]) & (PAin(6) == inPutOff))
            {
                PAout(8) = outPutOn;
            }
            if ((tempreture[3] > optData.tempretureStop[3]) || (PAin(6) == inPutOn))
            {
                PAout(8) = outPutOff;
            }
            if (tempreture[3] > optData.tempretureWarn[3])
            {
                tempretureWarnFlag = 1;
            }
        }
        else
        {
            PAout(8) = outPutOff;
        }
        //输送管加热
        if (PFin(4) == inPutOn)
        {
            if ((tempreture[3] < optData.tempretureStart[3]) & (PAin(1) == inPutOff))
            {
                PCout(9) = outPutOn;
            }
            if ((tempreture[3] > optData.tempretureStop[3]) || (PAin(1) == inPutOn))
            {
                PCout(9) = outPutOff;
            }
            if (tempreture[3] > optData.tempretureWarn[3])
            {
                tempretureWarnFlag = 1;
            }
        }
        else
        {
            PCout(9) = outPutOff;
        }
        //报警指示灯
        if (tempretureWarnFlag)
        {
            PGout(8) = outPutOn;
        }
        else
        {
            PGout(8) = outPutOff;
        }
    }
}
